package cn.edu.nju.sharing;

import java.io.File;

import cn.edu.nju.R;
import cn.edu.nju.cropimage.CropImage;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{
	private final int PHOTO_CUT_REQUEST = 100;
	private StartUpProvider provider;
	private ImageGetter imageGetter;
	private Button registerButton;
	private Button avatarButton;
	private ImageView avatarImage;
	private EditText usernameInput;
	private EditText passwordInput;
	private EditText confirmPasswordInput;
	private TextView errorText;
	private int AVATAR_SIZE = 50;
	private File avatarFile = null;
	private Uri avatarUri;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.register);
		provider = StartUpProvider.getInstance(this);
		imageGetter = new ImageGetter(this);
		
		// init widget
		avatarImage = (ImageView)findViewById(R.id.avatar_image);
		usernameInput = (EditText)findViewById(R.id.user_name_input);
		passwordInput = (EditText)findViewById(R.id.password_input);
		confirmPasswordInput = (EditText)findViewById(R.id.confirm_password_input);
		errorText = (TextView)findViewById(R.id.error_message);
		initAvatarButton();
		initRegisterButton();
	}
	
	private void initAvatarButton(){
		avatarButton = (Button)findViewById(R.id.change_avatar);
		avatarButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				imageGetter.startImageGetActivity();
			}
		});
	}
	
	private void initRegisterButton(){
		registerButton = (Button)findViewById(R.id.register_button);
		registerButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				// collect username and password
				String username = usernameInput.getText().toString();
				String password = passwordInput.getText().toString();
				String confirmPassword = confirmPasswordInput.getText().toString();
				boolean canRegister = true;
				
				if(canRegister && username.equals("")){
					errorText.setText(R.string.username_empty);
					canRegister = false;
				}
				
				if(canRegister && password.equals("")){
					errorText.setText(R.string.password_empty);
					canRegister = false;
				}
				
				if(canRegister && !password.equals(confirmPassword)){
					errorText.setText(R.string.password_not_equal);
					canRegister = false;
				}
				
				if(canRegister){
					boolean result = provider.register(username, password, avatarFile);
					if(result == false){
						errorText.setText(R.string.user_name_exist);
					}
					else{
						// back to main activity
						if(avatarFile != null){
							avatarFile.delete();
						}
						
						Intent intent = new Intent(RegisterActivity.this, StartUpActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
					}
				}
			}
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		if(requestCode != PHOTO_CUT_REQUEST){
			avatarUri = imageGetter.handleActivityResult(requestCode, resultCode, data);
			if(avatarUri != null){
				// crop image
				Intent intent = new Intent(RegisterActivity.this, CropImage.class);
				intent.putExtra("image-path", avatarUri.getPath());
				intent.putExtra("scale", true);
				startActivityForResult(intent, PHOTO_CUT_REQUEST);
			}
		}
		else{
			// set avatar image
			if(avatarFile != null){
				avatarFile.delete();
			}
			avatarFile = new File(avatarUri.getPath());
			Bitmap avatarBitmap = imageGetter.decodeFile(avatarFile, AVATAR_SIZE);
			avatarImage.setImageBitmap(avatarBitmap);
		}	
	}
}
